package net.tribal.comic.services

import net.tribal.comic.entities.Comic
import net.tribal.comic.entities.Page
import net.tribal.comic.entities.AlterPage
import net.tribal.comic.entities.Comment
import net.tribal.comic.entities.User


class CommentService {

    boolean transactional = true
	
    
	/**
	 * Search by title and user role
	 * 
	 * @param segment
	 * @param offset
	 * @param ord
	 * @param sor
	 * @param admin Boolean
	 *  -true: admin role
	 *  -false: user role
	 * @param custom String[optional]
	 * @return Map
	 *  -["data","count"]
	 */
	def getActiveComments ( segment, offset, ord, sor, admin, custom ) {
		ord = (ord)?  ord: "dateCreated"
		sor = (sor)?  sor: "desc"
		
		def result = [:]
		def c = Comments.createCriteria()
		def results = c.list {
			if( !admin ) { eq("disabled", false) }
			if( custom ) { ilike("title", "%"+custom+"%") }
			
			if(offset)
				firstResult(offset)
				
			maxResults(segment)
			
			order(ord, sor)
		}
		
		c = Comments.createCriteria()
		def total = c.count{
			if(!admin) { eq("disabled", false) }
			if(custom) { ilike("title", "%"+custom+"%") }
		}
		
		result["data"] = results
		result["count"] = total
		return result
	}
		
    
    /**
     * Vote positively or negatively
     * 
     * @param commentInstance
     * @param up 
     * 	- true: positive vote
     * 	- false: negative vote
     * @return Boolean
     * 	-successfully: true
     *  -error: false
     */
    def vote ( commentInstance, up ) {
    	if ( up ) {
    		commentInstance.up = commentInstance.up +1
    	} else {
    		commentInstance.down = commentInstance.down +1
    	}
    	
    	if ( commentInstance.save() && !commentInstance.hasErrors() ) {
    		return true
    	}
    	
    	return false
    }
    
    
    /**
     * Check this entity for review
     * 
     * @param commentInstance
     * @return Boolean
     * 	-successfully: true
     *  -error: false
     */
    def moderate ( commentInstance ) {
    	commentInstance.moderationRequest = true
    	
    	if ( commentInstance.save() && !commentInstance.hasErrors() ) {
    		return true
    	}
    	
    	return false
    }
    
    
    /**
     * 
     * @param commentInstance
     * @return commentInstance
     */
    def update ( commentInstance ) {
        if ( !commentInstance.hasErrors() ) {
        	commentInstance.save()
        }
        return commentInstance
    }
    
    
    /**
     * 
     * @param commentInstance
     * @param commentToName
     * @param commentToId
     * @return
     */
    def delete ( commentInstance, commentToName, commentToId ) {
	    try {
	        if( commentToName == 'user' ){
				def user = User.get( commentToId )
				user.removeFromUserComments( commentInstance )
			}else if(commentToName == 'page'){
				def page = Page.get( commentToId )
				page.removeFromComments( commentInstance )
			}else if(commentToName == 'comic'){
				def comic = Comic.get( commentToId )
				comic.removeFromComments( commentInstance )
			}else if(commentToName == 'alterPage'){
				def alterPage = AlterPage.get( commentToId )
				alterPage.removeFromComments( commentInstance )
			}else{
				commentInstance.delete()
			}
	        return true
	    }
	    catch (org.springframework.dao.DataIntegrityViolationException e) {
	    	log.error e
	    	return false
	    }
    }
}